package org.ww.product.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ww.common.utils.PageUtils;
import org.ww.common.utils.R;
import org.ww.product.entity.SkuInfoEntity;
import org.ww.product.service.SkuInfoService;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


/**
 * sku信息
 *
 * @author ww
 * @email ww@qq.com
 * @date 2024-01-05 13:25:18
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;


    @PostMapping("/price")
    public R getPrice(@RequestBody List<Long> skuIds){
        List<SkuInfoEntity> skuInfoEntities = skuInfoService.getByIds(skuIds);
        Map<Long, BigDecimal> skuPriceMap = Optional.ofNullable(skuInfoEntities)
                .orElseGet(ArrayList::new)
                .stream()
                .collect(Collectors.toMap(SkuInfoEntity::getSkuId, SkuInfoEntity::getPrice));
        return R.ok().setData(skuPriceMap);
    }

    /**
     * 根据skuId批量查询
     */
    @PostMapping("/get/sku-ids")
    public R getBySkuIds(@RequestBody List<Long> skuIds){
        List<SkuInfoEntity> wareSkuEntities = skuInfoService.getByIds(skuIds);

        return R.ok().put("data", wareSkuEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = skuInfoService.queryPageByParams(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{skuId}")
    public R info(@PathVariable("skuId") Long skuId){
		SkuInfoEntity skuInfo = skuInfoService.getById(skuId);

        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.save(skuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] skuIds){
		skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

}
